/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_MULTI_TRANSFORMATION_H
#define IMAGE_KNIFE_C_MULTI_TRANSFORMATION_H

#include "imageknife_data_napi.h"
#include "transformation/transformation.h"
#include "utils/log.h"
#include "utils/pixelmap_utils.h"
#include "imageknife_task_internal.h"
#include "transformation/gputransform/imageknife_egl.h"
namespace ImageKnifePro {

MultiTransformation::MultiTransformation(std::vector<std::shared_ptr<Transformation>> transformations)
    : transformations_(transformations)
{
}

void MultiTransformation::Transform(std::shared_ptr<ImageKnifeTask> task)
{
    auto taskInternal = std::dynamic_pointer_cast<ImageKnifeTaskInternal>(task);
    IMAGE_KNIFE_CHECK(taskInternal == nullptr);
    auto request = taskInternal->GetImageKnifeRequestInternal();
    IMAGE_KNIFE_CHECK(request == nullptr);
    auto option = request->GetImageKnifeOption();
    IMAGE_KNIFE_CHECK(option == nullptr);

    // 为当前线程创建EGL上下文
    ImageKnifeEGL egl;
    IMAGE_KNIFE_CHECK(!egl.Init(), "EGL Init Failed");

    for (auto transformation : transformations_) {
        if (transformation == nullptr) {
            task->EchoError("Empty transformation");
            continue;
        }

        if (request->IsDestroy() || taskInternal->IsFatalErrorHappened()) {
            IMAGE_KNIFE_LOG(LOG_DEBUG, "%{public}s Transformation Canceled", taskInternal->GetTaskInfo().c_str());
            return;
        }

        IMAGE_KNIFE_LOG(LOG_DEBUG, "%{public}s Transform: %{public}s", taskInternal->GetTaskInfo().c_str(),
                        transformation->GetTransformInfo().c_str());
        // 采集主图执行图形变换的信息
        TransformationInfo info = {.desc = transformation->GetTransformInfo(),
                                   .startTime = ImageKnifeDataNapi::GetTimestamp()};
        // 执行图形变换
        bool result = transformation->Transform(task);
        info.endTime = ImageKnifeDataNapi::GetTimestamp();
        info.isSuccess = result;
        request->GetImageInfo().transformationInfos.emplace_back(std::move(info));
        if (!result) {
            return;
        }
        PixelmapUtils::SetMemoryName(task->product.imageData, option->pixelName);
    }
}

std::string MultiTransformation::GetTransformInfo()
{
    std::string info;
    for (auto transformation : transformations_) {
        if (transformation == nullptr) {
            continue;
        }
        info += transformation->GetTransformInfo() + ";";
    }
    return info;
}

} // end of name
#endif // IMAGE_KNIFE_C_MULTI_TRANSFORMATION_H
